package com.loadmore.common.schedule

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle

/**
 * @author sunleilei
 * @date 2018.8.2
 *
 */
object AlarmUtil {

    /**
     * @param context
     * @param id 日程id
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param remindType 提醒类型 ["不提醒", "事件发生时", "提前10分钟","提前30分钟","提前一小时","提前一天"]
     * @param repeatType 重复类型 ["一次性活动", "每分钟","每小时", "每天", "每个工作日","每周","每月","每年"]
     */
    fun setAlarm(context: Context,id:Long,startTime:Long,endTime:Long,remindType:Int,repeatType:Int) {
        when(remindType) {
            1->{
                val alarmManager:AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
                val intent = Intent(context,AlarmReceiver::class.java)
                intent.action = AlarmActions.alarm_clock+"."+id
                val bundle = Bundle()
                bundle.putLong("id",id)
                intent.putExtras(bundle)
                val pendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
                //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,startTime,AlarmManager.INTERVAL_DAY,pendingIntent)
               // alarmManager.set(AlarmManager.RTC_WAKEUP,startTime,pendingIntent)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    alarmManager.setWindow(AlarmManager.RTC_WAKEUP,startTime,0,pendingIntent)
                }
            }
        }

    }

    fun clearAlarm(context: Context,id:Long) {

        val alarmManager:AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent(context,AlarmReceiver::class.java)
        intent.action = AlarmActions.alarm_clock+"."+id
        val pendingIntent = PendingIntent.getBroadcast(context,0,intent,0)
        alarmManager.cancel(pendingIntent)
    }
}